package com.topscore.uke.mvp.presenters;

import android.graphics.Color;

import com.topscore.uke.entities.PerformanceChartsEntity;
import com.topscore.uke.entities.PerformanceData;
import com.topscore.uke.mvp.contractors.GetTeamPerformanceContractor;
import com.topscore.uke.mvp.model.GetTeamPerformanceModel;
import com.topscore.uke.network.BaseResponse;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.ComboLineColumnChartData;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.SubcolumnValue;

/**
 * Created by 71468 on 2017/10/20.
 */

public class GetTeamPerformancePresenter implements GetTeamPerformanceContractor, GetTeamPerformanceContractor.Presenter {
    private View mView;

    public GetTeamPerformancePresenter(View view) {
        this.mView = view;
    }

    @Override
    public void getPerformanceInYear(String year, String groupCode) {
        mView.onLoading("正在获取本年销售业绩");
        new GetTeamPerformanceModel(groupCode)
                .setCallBack(new CallBack() {
                    @Override
                    public void onGetDataSuccess(BaseResponse<PerformanceData> response) {
                        mView.onGetPerformanceSuccess(response.getData());
                        mView.onGetPerformanceCharDataSuccess(getCharData(response.getData().getSaleCounts(), response.getData().getSaleMoneys()));
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                        mView.onFail(errorCode, errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        mView.onLoadFinished();
                    }
                })
                .getPerformanceInYear(year);
    }

    @Override
    public void getPerformanceInMonth(String month, String groupCode) {
        mView.onLoading("正在获取本月销售业绩");
        new GetTeamPerformanceModel(groupCode)
                .setCallBack(new CallBack() {
                    @Override
                    public void onGetDataSuccess(BaseResponse<PerformanceData> response) {
                        mView.onGetPerformanceSuccess(response.getData());
                        mView.onGetPerformanceCharDataSuccess(getCharData(response.getData().getSaleCounts(),response.getData().getSaleMoneys()));
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                        mView.onFail(errorCode, errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        mView.onLoadFinished();
                    }
                })
                .getPerformanceInMonth(month);
    }

    private ComboLineColumnChartData getCharData(List<PerformanceChartsEntity> saleCounts, List<PerformanceChartsEntity> saleMoneys) {
        ComboLineColumnChartData chartData = new ComboLineColumnChartData(getColumnData(saleMoneys), getLineData(saleCounts));
        List<AxisValue> axisXValues = new ArrayList<>();
        for (int i = 0; i < saleCounts.size(); i++) {
            AxisValue axisXValue = new AxisValue(i).setLabel(saleCounts.get(i).getX());
            axisXValues.add(axisXValue);
        }
        Axis axisX = new Axis(axisXValues);
        axisX.setHasTiltedLabels(false);
        chartData.setAxisXBottom(axisX);
        return chartData;
    }

    private LineChartData getLineData(List<PerformanceChartsEntity> saleCounts) {
        List<Line> lines = new ArrayList<>();
        List<PointValue> pointValues = new ArrayList<>();
        for (int i = 0; i < saleCounts.size(); i++) {
            PointValue value = new PointValue(i, saleCounts.get(i).getY());
            pointValues.add(value);
        }
        Line line = new Line(pointValues);
        line.setPointRadius(3);
        line.setStrokeWidth(1);
        line.setColor(Color.parseColor("#f8b551"));
        lines.add(line);
        LineChartData lineChartData = new LineChartData(lines);
        return lineChartData;
    }

    private ColumnChartData getColumnData(List<PerformanceChartsEntity> saleMoneys) {
        List<Column> columns = new ArrayList<>();
        for (int i = 0; i < saleMoneys.size(); i++) {
            SubcolumnValue subcolumnValue = new SubcolumnValue(saleMoneys.get(i).getY());
            subcolumnValue.setColor(Color.parseColor("#CE3B3E"));
            List<SubcolumnValue> subcolumnValues = new ArrayList<>();
            subcolumnValues.add(subcolumnValue);
            Column column = new Column(subcolumnValues);
            columns.add(column);
        }
        ColumnChartData columnChartData = new ColumnChartData(columns);
        return columnChartData;
    }
}
